from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from ..models import Department, UserInfo, PrettyNum, Task
from django import forms
from django.forms import Form, ModelForm
from django.utils.safestring import mark_safe
from ..utils.pagination import Pagination
from ..utils.bootstrap import BootStrapModelForm
from ..utils.form import UserModelForm, PrettynumModelForm, PrettynumEditModelForm
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django.views.decorators.csrf import csrf_exempt
import json

class TaskModelForm(BootStrapModelForm):
    class Meta:
        model = Task
        fields = '__all__'
        widgets = {
            'detail': forms.TextInput()
        }

def task_list(request):
    form = TaskModelForm()
    return render(request, 'task_list.html', {'form': form})

@csrf_exempt
def task_ajax(request):
    data_dict = {'status': True, 'data': [11, 22, 33 ,44]}
    return JsonResponse(data_dict)

@csrf_exempt
def task_add(request):
    form = TaskModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        data_dict = {'status': True}
        return HttpResponse(json.dumps(data_dict))
    data_dict = {'status': True, 'error': form.errors}
    return HttpResponse(json.dumps(data_dict))